package com.jhd.fuzhou.view.knowledge;

import android.content.Context;
import android.text.Html;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.fastjson.JSON;
import com.jhd.fuzhou.R;
import com.jhd.fuzhou.adapter.ArchiveAdapter;
import com.jhd.fuzhou.service.ArchiveService;
import com.jhd.fuzhou.service.KnowledgeBaseService;
import com.jhd.fuzhou.util.UtilHelper;
import com.jhd.fuzhou.view.login.BaseActivity;
import com.jhd.fuzhou.vo.ArchivesFileVo;
import com.jhd.fuzhou.vo.JsonForResult;
import com.jhd.fuzhou.vo.KnowledgeVo;

import net.zigin.commons.util.ParamsMapUtil;
import net.zigin.commons.util.StringUtil;

import java.util.ArrayList;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class KnowledgeDetailActivity extends BaseActivity {

    private KnowledgeVo knowledgeVo;
    private Context context=this;
    @BindView(R.id.tv_knowledge_base_name)TextView tv_knowledge_base_name;
    @BindView(R.id.tv_knowledge_base_type)TextView tv_knowledge_base_type;
    @BindView(R.id.tv_knowledge_base_dept)TextView tv_knowledge_base_dept;
    @BindView(R.id.tv_issue_date)TextView tv_issue_date;
    @BindView(R.id.tv_effective_date)TextView tv_effective_date;
    @BindView(R.id.tv_version_number)TextView tv_version_number;
    @BindView(R.id.tv_detail)TextView tv_detail;
    @BindView(R.id.tv_status)TextView tv_status;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_knowledge_detail);
    }

    @Override
    protected void processLogic() {
        String id=getIntent().getStringExtra("id");
        getKnowledgeById(id);
    }

    private void setDate(KnowledgeVo knowledgeVo){
        onBack();
        tv_detail.setMaxLines(3);
        if(!StringUtil.isNullOrEmpty(knowledgeVo.getContent())) tv_detail.setText(Html.fromHtml(knowledgeVo.getContent()));
        tv_knowledge_base_name.setText(StringUtil.isNullOrEmpty(knowledgeVo.getName())?"":knowledgeVo.getName());
        tv_knowledge_base_type.setText(StringUtil.isNullOrEmpty(knowledgeVo.getTypeName())?"":knowledgeVo.getTypeName());
        tv_knowledge_base_dept.setText(StringUtil.isNullOrEmpty(knowledgeVo.getPublisher())?"":knowledgeVo.getPublisher());
        if(!StringUtil.isNullOrEmpty(knowledgeVo.getPublishDate()))tv_issue_date.setText(knowledgeVo.getPublishDate().substring(0,10));
        if(!StringUtil.isNullOrEmpty(knowledgeVo.getEffect()))tv_effective_date.setText(knowledgeVo.getEffect().substring(0,10));
        tv_version_number.setText(StringUtil.isNullOrEmpty(knowledgeVo.getVersion())?"":knowledgeVo.getVersion());
    }

    @OnClick({R.id.tv_status})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.tv_status:
                if(tv_status.getText().toString().equals("显示全部")){
                    tv_status.setText("隐藏");
                    tv_detail.setMaxLines(10000);
                }else{
                    tv_status.setText("显示全部");
                    tv_detail.setMaxLines(3);
                }
                break;

        }
    }

    public void getKnowledgeById(String id){
        Retrofit retrofit = UtilHelper.retrofit(application, UtilHelper.baseUrls.API());//URL的头
        KnowledgeBaseService vs = retrofit.create(KnowledgeBaseService.class);
        Map<String, String> params = ParamsMapUtil.getInstance("id",id);
        Call<JsonForResult> jsonForResultCall = vs.getKnowledgeById(application.getAccessToken(),params);//调用接口获取json返回
        jsonForResultCall.enqueue(new Callback<JsonForResult>() {
            @Override
            public void onResponse(Call<JsonForResult> call, Response<JsonForResult> response) {
                try {
                    JsonForResult jsonForResult = (JsonForResult) response.body();
                    if (jsonForResult != null && jsonForResult.isSuccess()) {
                        if(jsonForResult.getData()!=null){
                            knowledgeVo = JSON.parseObject(jsonForResult.getData().toString(), KnowledgeVo.class);
                            setDate(knowledgeVo);
                        }
                    } else {
                        UtilHelper.alertMsg(context, "错误", jsonForResult.getMessage());
                    }
                } catch (Exception e) {
//                    CustomToast.showText(context,e.getLocalizedMessage(),3000,0,0).show();
                }
            }

            @Override
            public void onFailure(Call<JsonForResult> call, Throwable t) {
                String msg = "网络异常!";
                if (StringUtil.isNullOrEmpty(t.getMessage()) == false) msg = msg;
                UtilHelper.alertMsg(context, "错误", msg);
            }
        });
    }
}